package view.panel;

import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;

import exception.NotFoundException;

import manager.MenuManager;
import vo.Menu;

public class MenuLeftPanel extends JPanel {

	JTextField jfieldMenuSearch, jfieldMenuName, jfieldPrice;
	JButton jbtnSearch, jbtnNewMenu,jbtnDelMenu,jbtnSaveMenu;

	JLabel jlabelMenuName, jlabelPrice;


	public MenuLeftPanel() {

		setLayout(new FlowLayout(FlowLayout.LEFT, 10, 30));
		setSize(300,200);
		setVisible(true);

		jbtnSearch = new JButton("메뉴 검색");
		jbtnNewMenu = new JButton("새 메뉴");
		jbtnDelMenu = new JButton("삭제");
		jbtnSaveMenu = new JButton("저장");

		jlabelMenuName = new JLabel("메뉴명");
		jlabelPrice = new JLabel("가격");

		jfieldMenuSearch = new JTextField(8);
		jfieldMenuName = new JTextField(8);
		jfieldPrice = new JTextField(8);

		add(jfieldMenuSearch);
		add(jbtnSearch);
		add(jlabelMenuName);
		add(jfieldMenuName);
		add(jlabelPrice);
		add(jfieldPrice);
		add(jbtnNewMenu);
		add(jbtnDelMenu);
		add(jbtnSaveMenu);

		jbtnNewMenu.addMouseListener(new MouseInputAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String blank = "";

				jfieldMenuName.setText(blank);
				jfieldPrice.setText(blank);
				invalidate();
			}
		});

		jbtnDelMenu.addMouseListener(new MouseInputAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				
				String menuName = jfieldMenuSearch.getText();
				
				try {
					MenuManager.getInstance().deleteMenu(menuName);
				} catch (NotFoundException e1) {
					e1.printStackTrace();
				}
				
			}
		});

		jbtnSaveMenu.addMouseListener(new MouseInputAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				
				String menuName = jfieldMenuSearch.getText();
				int price = Integer.parseInt(jfieldPrice.getText());
				
				Menu menu = new Menu();
				
				menu.setName(menuName);
				menu.setPrice(price);
				
				try {
					MenuManager.getInstance().saveMenu(menu);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});

		jbtnSearch.addMouseListener(new MouseInputAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				
				String menuName = jfieldMenuSearch.getText();
				
				try {
					Menu menu = MenuManager.getInstance().searchMenu(menuName);
					
					jfieldMenuName.setText(menu.getName());
					jfieldPrice.setText(String.valueOf(menu.getPrice()));					
					
				} catch (NotFoundException e1) {
					System.out.println(e1.getMessage());
				}
				
	
			}

		});
	}
}


